﻿/**
 * @author : v_huang 
 * @email  : asd5640252@gmail.com
 * @blog   : http://wwww.xiaoguizi.net
 *
 * Copyright (c) 2012, koala
 * update 2012-03-29
 */

(function ($) {
this.G = function () {
 	_G = this.G;
 }
var window = this,
 	doc = window.document,
 	$doc = $( doc ),
 	$win = $( window );

G.CONFIG = {
	AUTHOR: 'asd5640252@gmail.com',
	VERSION: '1.0'
}
G._util = {};

if ($ && (parseFloat($.fn.jquery) < 1.4)) {
	alert('需要jquery1.4以上版本支持！');
}
(function (util) {
	var goTop = function() {
	 		var scrollDiv = document.createElement('div');
			$(scrollDiv).attr({'id':'toTop', 'title': '回到顶部'}).appendTo('body');
			$('#toTop').hide();
			$win.scroll(function () {
				if ($doc.scrollTop() != 0) {
					$('#toTop').fadeIn();
				} else {
					$('#toTop').fadeOut();
				}
			});
			$('#toTop').click(function () {
				$win.scrollTop(0);
			});
	};
	util.goTop = goTop;
})(G._util);

(function (util) {
	var rotate = {
		curImg : {},
		animateQueen : [],
		threeDRotate : function (parm) {
			var rotateFn,
			defaultOptions = {
				parent: "threDObj",
				width: '',
				height: '',
				time: 1000,
				type: 'hr'
			},
			parm = $.extend(defaultOptions, parm),
			parentElem = doc.getElementById(parm.parent),
			imgList = parentElem.getElementsByTagName('img'),
			width = !!parm.width ? parm.width : parseInt($(imgList[0]).css('width')),
			height = !!parm.height ? parm.height : parseInt($(imgList[0]).css('height'));
			if (parm.type == 'hr') {
				rotateFn = rotate.HrRotate;
			}
			rotate.curImg = imgList[0];
	 		for (var i = 0,len = imgList.length; i < len; i++) {
	 			imgList[i].style.display = 'none';
	 			imgList[i].style.position = 'relative';
		        rotate.animateQueen.push(imgList[i]);
	 		}
	 		imgList[0].style.display = 'block';
			rotateFn(imgList, width, height, parm.time);

		},
		HrRotate : function (list, width, height, time) {
			var counter = '',
				step = '';
			if (arguments.length !== 4 || rotate.animateQueen.length == 0) {
				return;
			}
	 		var from = rotate.curImg;
	 		var to = rotate.animateQueen[1];
	 		rotate.curImg = to;
	 		from.style.display = 'block';
	 		to.style.display = 'none';
	 		counter = 100;
	 		step = parseInt((counter*80) / time);
	 		if (step <= 0 ) {
	 			alert('动画时间不能超过8000毫秒，请重设！');
	 			return;
	 		}
	 		animatePause = setInterval( function () {
		 		clearInterval(animatePause);
		 		hr_interval = setInterval( function() {
		 			if (counter < 0) {
		 				from.style.width = 0 + 'px';
		                from.style.left = width/4 + 'px';
		                from.style.display = 'none';
		                clearInterval(hr_interval);
		                counter_1 = 0;
		                hr_interval = setInterval(function(){
		                    if(counter_1>100){
		                        clearInterval(hr_interval);
		                        to.style.width = width + 'px';
		                        to.style.left = 0 + 'px';
					 			rotate.animateQueen.shift();
					 			rotate.animateQueen.push(from);	
		                        if(rotate.animateQueen.length!=0){
		                            rotate.HrRotate(list, width, height, time);
		                        }
		                    }else{
		                        to.style.width = counter_1/100*width + 'px';
		                        to.style.left = (100-counter_1)/200*width + 'px';
				                to.style.display = 'block';
		                        counter_1 += step;
		                    }
		                },40);
		 			} else{
		 				from.style.width = parseInt(counter/100 * width) + 'px';  
		 				from.style.left = parseInt((100-counter)/200*width) + 'px';
		 				counter -= step;
		 			}
		 		}, 40 );
			},3000 );
		}
		
	}
	util.threeDRotate = rotate.threeDRotate;
})(G._util);
window.G = G._util;
})( jQuery );